package com.yenlien.traceyun.control.service;

import com.yenlien.traceyun.control.dao.ReportDao;
import com.yenlien.traceyun.control.entity.Report;
import com.yenlien.traceyun.webase.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ReportService {

    @Autowired
    private ReportDao reportDao;

    /**
     * 新增举报
     *
     * @param report
     */
    @Transactional
    public void save(Report report) {
        report.setReportTime(new Date());
        report.setState("未回复");
        if (!StringUtils.isEmpty(report)) {
            reportDao.save(report);
        }
    }

    /**
     * 查询举报
     *
     * @param page
     * @return
     */
    public Page<Report> select(Integer page) {
        Sort ageSort = Sort.by(Sort.Direction.DESC, "reportTime");
        PageRequest pageRequest = PageRequest.of(page, 10, ageSort);
        Page<Report> all = reportDao.findAll(pageRequest);
        return all;
    }

    public Page<Report> findByList(Report report) {
        Pageable pageable = PageRequest.of(report.getPage(), report.getPageSize(), Sort.Direction.DESC, ("reportTime"));
        return reportDao.findAll((Root<Report> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
            List<Predicate> list = new ArrayList<>();
            if(org.apache.commons.lang3.StringUtils.isNotBlank(report.getHeadline()))
                list.add(cb.like(root.get("headline"), "%" + report.getHeadline() + "%"));
            if(org.apache.commons.lang3.StringUtils.isNotBlank(report.getProductName()))
                list.add(cb.like(root.get("productName"), "%" + report.getProductName() + "%"));
            return cb.and(list.toArray(new Predicate[list.size()]));
        }, pageable);
    }

    /**
     * 根据标题模糊搜索
     *
     * @param headlines
     * @return
     */
    public Object search(String headlines) {
        return reportDao.findByHeadlineLike(headlines);
    }

    public Object findAll() {
        return reportDao.findAll();
    }

    /**
     * 根据id查询意见
     *
     * @param id
     * @return
     */
    public Object selectId(String id) {
        return reportDao.findById(id);
    }

    /**
     * 根据id修改投诉举报
     *
     * @param report
     * @return
     */
    @Transactional
    public void update(Report report) {
        report.setState("已回复");
        report.setReplyTime(new Date());
        if (!StringUtils.isEmpty(report)) {
            reportDao.save(report);
        } else {
            throw new BusinessException("500", "修改失败");
        }
    }


}
